package vip.mtdp.customer.service.rpc.impl;

import java.time.LocalDateTime;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import vip.mtdp.common.CommonConstant;
import vip.mtdp.customer.api.PartyEnum;
import vip.mtdp.customer.dal.dao.MerchantMapper;
import vip.mtdp.customer.dal.dao.PartyMapper;
import vip.mtdp.customer.dal.domain.Merchant;
import vip.mtdp.customer.dal.domain.Party;
import vip.mtdp.customer.api.dto.MerchantDTO;
import vip.mtdp.customer.api.dto.PartyDTO;
import vip.mtdp.customer.api.MerchantRpcService;

/**
 * 
 *
 * @Description 商户服务
 * @author wangguoqing
 * @date 2017年9月17日上午10:19:03
 *
 */
@Slf4j
@Service
public class MerchantRpcServiceImpl implements MerchantRpcService {

	@Autowired
	private PartyMapper partyMapper;
	
	@Autowired
	private MerchantMapper merchantMapper;

	@Override
	public MerchantDTO createMerchant(MerchantDTO merchant) {
		log.debug("创建商户");
		Party party = new Party();
		//生成商户号
		String no = String.valueOf(System.nanoTime());
		party.setNo(no);
		party.setName(merchant.getPartyDTO().getName());
		party.setType(PartyEnum.MERCHANT.getValue());
		party.setState(CommonConstant.ENALBED);
		LocalDateTime d = LocalDateTime.now();
		party.setCreatedAt(d);
		party.setUpdatedAt(d);
		int cnt = this.partyMapper.save(party);
		if(cnt == 1){
			Merchant m = new Merchant();
			BeanUtils.copyProperties(merchant, m);
			m.setPartyId(party.getOid());
			m.setCreatedAt(d);
			m.setUpdatedAt(d);
			this.merchantMapper.save(m);
			merchant.setPartyId(party.getOid());
			merchant.setId(m.getOid());
		}
		return merchant;
	}

	/* 应用实现
	@Override
	public MerchantDTO openMerchantAccountNo(MerchantDTO merchant) {
		log.debug("开通商户账户");
		this.createMerchant(merchant);
		UserDTO userDTO = new UserDTO();
		userDTO.setPartyId(merchant.getPartyId());
		userDTO.setMerchantId(merchant.getId());
		userDTO.setUserName(merchant.getContactMobile());
		//密码明文
		userDTO.setLoginPwd(merchant.getContactMobile());
		userDTO.setRegTime(new Date());
		userDTO.setRegSource("sys");
		this.userService.createUser(userDTO);
		List<UserDTO> users = new ArrayList<UserDTO>();
		users.add(userDTO);
		merchant.setUsers(users);
		return merchant;
	}
	*/

	@Override
	public boolean updateMerchant(MerchantDTO merchant) {
		log.debug("更新商户");
		String name = merchant.getPartyDTO().getName();
		Integer status = merchant.getPartyDTO().getState();
		Long partyId = merchant.getPartyId();
		if(partyId != null){
			Party party = new Party();
			party.setOid(partyId);
			party.setName(name);
			party.setState(status);
			int c = this.partyMapper.update(party);
			//更新商户基本信息失败直接返回失败
			if(c != 1){
				return false;
			}
		}
		Merchant m = new Merchant();
		BeanUtils.copyProperties(merchant, m);
		int cnt = this.merchantMapper.update(m);
		if(cnt == 1){
			return true;
		}
		return false;
	}

	@Override
	public MerchantDTO getMerchantByPartyId(Long partyId) {
		log.debug("根据客户Id查询商户partyId={}",partyId);
		Merchant m = merchantMapper.getMerchantByPartyId(partyId);
		MerchantDTO merchantDTO = new MerchantDTO();
		BeanUtils.copyProperties(m, merchantDTO);
		PartyDTO partyDTO = new PartyDTO();
		BeanUtils.copyProperties(m.getParty(),partyDTO);
		merchantDTO.setPartyDTO(partyDTO);
		return merchantDTO;
	}

}